<template>
    <div class="data_list">
        <OrgTable ref="OrgTabRef" v-if="state.load" :get-data="pageList" :form-data="state.form" :remove="remove">
            <template v-slot:from_search>
                <el-form-item>
                    <el-select v-model="state.form.batchNumberId" placeholder="批次号" clearable style="width: 200px">
                        <el-option v-for="item in state.batchNumList" :key="item.id" :label="item.batchNumber"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-date-picker v-model="state.form.daterange" type="daterange" range-separator="至"
                        value-format="YYYY-MM-DD" start-placeholder="采集开始日期" end-placeholder="采集结束日期"
                        style="width: 260px" @change="daterangeChange" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search" :icon="'Search'">查询</el-button>
                    <el-button @click="download" :icon="'Download'" >下载</el-button>
                </el-form-item>
            </template>
            <template v-slot:columns>
                <el-table-column property="batchNumber" label="批次号" align="center" />
                <template v-for="(item, index) in state.header" :key="index">
                    <el-table-column :property="item.field" :label="item.label" align="center" />
                </template>
                <el-table-column property="deptName" label="组织" align="center" />
                <el-table-column property="createName" label="采集人" align="center" />
                <el-table-column property="create_time" label="采集时间" align="center">
                    <template #default="scope">
                        {{ dayjs(scope.row.create_time).format('YYYY-MM-DD HH:mm:ss') }}
                    </template>
                </el-table-column>
            </template>
        </OrgTable>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted, nextTick } from "vue"
import { useRouter, useRoute } from 'vue-router'
import OrgTable from '@/components/OrgTable.vue'
import { getTableHeader, page as pageList } from '@/api/data/data'
import { list as batchNumList } from '@/api/enact/batch-number'
import { dayjs } from 'element-plus'
import fileDownloader from '@/utils/fileDownloader';

const router = useRouter()
const route = useRoute()
const id = route.params.id
const OrgTabRef = ref(null)

const state = reactive({
    load: false,
    batchNumList: [],
    header: [],
    form: {
        tableId: id,
        batchNumberId: undefined,
        daterange: undefined,
        collectStart: undefined,
        collectEnd: undefined,
    }
})

const search = () => {
    OrgTabRef.value.queryData()
}

const daterangeChange = (val) => {
    if (val) {
        state.form.collectStart = val[0] + ' 00:00:00'
        state.form.collectEnd = val[0] + ' 23:59:59'
    } else {
        state.form.collectStart = undefined
        state.form.collectEnd = undefined
    }
}

const download = () => {
     fileDownloader.download({
        url: '/data/download',
        method: 'POST',
        data: state.form, 
        filename: '报表数据.xlsx'
    });
}

const getHeader = () => {
    getTableHeader(id).then(data => {
        state.header.push(...data)
        state.load = true
        // nextTick(() => {
        // })
    })
}

const getBatchNum = () => {
    batchNumList(id).then(data => {
        state.batchNumList = data
    })
}



onMounted(() => {
    getHeader()
    getBatchNum()
})
</script>
<style scoped>
.data_list {
    height: 100%;
}
</style>
